package org.jeecg.tender.keywordsExtraction.extractor;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.tender.keywordsExtraction.entity.TenderInformationExtractor;
import org.jeecg.tender.keywordsExtraction.vo.TenderInformationExtractorVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InformationExtractor {

    public static void main(String[] args) {

        String str = "模预估不含税金额37536万元预估增值税税率615项目的实施时间和地点项目周期自合同签订之";
        extractAmounts(str, null, "(\\d+(?:\\.\\d+)?)\\s*(?:万元|千元|元)?.*?(不含税|未含税|未含税总价|不含税总价|不含税金额)|(不含税|未含税|未含税总价|不含税总价|不含税金额).*?(\\d+(?:\\.\\d+)?)\\s*(?:万元|千元|元)?");

    }

    
    public static String extractTenderTitle2projectName(String tenderTitle, String regex) {
        if (tenderTitle == null || tenderTitle.isEmpty()) {
            return null;
        }
        // 正则表达式匹配"（项目编号"前面的内容
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(tenderTitle);

        if(matcher.find()) {
            return matcher.group(1);
        }
        return tenderTitle;
    }

    public static String extractTenderDesc2projectCode(String tenderDesc, String regex) {
        if (tenderDesc == null || tenderDesc.isEmpty()) {
            return null;
        }
        // 正则表达式匹配"项目编号："后的内容 "(?:CHAgS:|项目编号[:：]|项目编码[:：]|项目编号\\s*[:：])\\s*[（(]?([A-Za-z0-9]{10,20})[）)]?"
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(tenderDesc);

        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }


    public static Map<String, List<String>> extractAmounts(String text, String withTaxRegex, String withoutTaxRegex) {
        Map<String, List<String>> result = new HashMap<>();
        result.put("withTax", new ArrayList<>());
        result.put("withoutTax", new ArrayList<>());

        // 匹配所有金额数字（含税基准） (?:应答报价|项目预算|最高应答限价|金额|限价为|预算金额|预算|限价)(\d+(?:\.\d+)?)
        Pattern amountPattern = Pattern.compile(withTaxRegex);
        Matcher matcher = amountPattern.matcher(text);
        while (matcher.find()) {
            result.get("withTax").add(matcher.group(1));
        }

        // 处理显式标记的含税/不含税金额 (\d+(?:\.\d+)?)\(?(含税|不含税|未含税)\)?
        Pattern taxPattern = Pattern.compile(withoutTaxRegex);
        Matcher matcher2 = taxPattern.matcher(text);
        while (matcher2.find()) {
            if (matcher2.group(1) != null) {
                result.get("withoutTax").add(matcher2.group(1));
            } else {
                result.get("withoutTax").add(matcher2.group(4));
            }
        }
        return result;
    }


    public static TenderInformationExtractorVO tenderInformationExtractor2TenderInformationExtractorVO(TenderInformationExtractor tender){
        TenderInformationExtractorVO tenderInformationExtractorVO = new TenderInformationExtractorVO();
        tenderInformationExtractorVO.setAnnouncementType(extractorVOStr(tender.getNoticeType()));
        tenderInformationExtractorVO.setTitle(extractorVOStr(tender.getNoticeTitle()));
        tenderInformationExtractorVO.setPurchasingDepartment(extractorVOStr(tender.getPurchaser()));
        tenderInformationExtractorVO.setWinningBidder(extractorVOStr(tender.getSupplier()));
        tenderInformationExtractorVO.setProjectName(extractorVOStr(tender.getProjectName()));
        tenderInformationExtractorVO.setProjectNumber(extractorVOStr(tender.getProjectCode()));
        tenderInformationExtractorVO.setPriceIncludingTax(extractorVOStr(tender.getAmountWithTax()));
        tenderInformationExtractorVO.setPriceExcludingTax(extractorVOStr(tender.getAmountWithoutTax()));
        if (tender.getPublishTime() != null){
            tenderInformationExtractorVO.setPublishTime(DateUtils.formatDate(tender.getPublishTime(), DateUtils.DATE_TIME_PATTERN));
        }else {
            tenderInformationExtractorVO.setPublishTime("");
        }
        tenderInformationExtractorVO.setAnnouncementLink(extractorVOStr(tender.getNoticeUrl()));
        return tenderInformationExtractorVO;
    }

    public static String extractorVOStr(String str){
        if (StringUtils.isBlank(str)){
            return "";
        }
        return str;
    }
}
